﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.IO;

public partial class admin_buscadorMPower : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = null;
        if (Session["userid"] == null) Response.Redirect("../default.aspx");
        ArrayList listado_arr = null;
        ListItem item = null;
        if (!Page.IsPostBack)
        {

            lb_agent.Items.Clear();


            listado_arr = (ArrayList)DB.getListado("tbl_usuario_descripcion, tbl_usuario_perfil", "ude_nombre+' '+ude_apellido, UDE_ID", "upe_ude_id=ude_id and upe_per_id=3 and upe_estado=1");
            if ((listado_arr != null) && (listado_arr.Count > 0))
            {
                item = new ListItem("ALL", "");
                lb_agent.Items.Add(item);
                foreach (RE_GenericBean rgb in listado_arr)
                {
                    item = new ListItem(rgb.strC1, rgb.strC2);
                    lb_agent.Items.Add(item);
                }
            }

            string where = " (CAST(dat_xml AS VARCHAR(8000)) LIKE '%<client>MPowerEnergy</client>%')";
            ds = (DataSet)DB.getDataMPower(where);
            gvd.DataSource = ds;
            gvd.DataBind();
            Session["where"] = ds;

            int tosimple = DB.getCountbyDayMP("E");
            int todouble = DB.getCountbyDayMP("EG");
            int wesimple = DB.getCountbyWeekMP("E");
            int wedouble = DB.getCountbyWeekMP("EG");
            int tototal = tosimple + (todouble * 2);
            int wetotal = wesimple + (wedouble * 2);
            int simpletotal = DB.getTotalApprovedMP("E");
            int doubletotal = DB.getTotalApprovedMP("EG");
            int grandtotal = simpletotal + (doubletotal * 2);

            lb_tosimple.Text = tosimple.ToString();
            lb_todouble.Text = todouble.ToString();
            lb_wesimple.Text = wesimple.ToString();
            lb_wedouble.Text = wedouble.ToString();
            lb_tototal.Text = tototal.ToString();
            lb_wetotal.Text = wetotal.ToString();
            lb_simpletotal.Text = simpletotal.ToString();
            lb_dobletotal.Text = doubletotal.ToString();
            grantotal.Text = grandtotal.ToString();
        }
        

    }
    protected void bt_search_Click(object sender, EventArgs e)
    {
        string where = "dat_id>0";
        if (!lb_agent.SelectedValue.Trim().Equals("")) where += " and CAST(dat_xml AS VARCHAR(8000)) like ('%<frmAgentName>" + lb_agent.SelectedValue + "</frmAgentName>%')";
        where += " and  (CAST(dat_xml AS VARCHAR(8000)) LIKE '%<client>MPowerEnergy</client>%')";
        //  and dat_xml.query('//campos/frmQCStatus').value('.', 'varchar(max)')='APPROVED'";
        if (!tb_startdate.Text.Equals("")) { where += " and dat_xml.query('//campos/frmQCDate').value('.', 'date')>='" + DateTime.Parse(tb_startdate.Text) + "'"; }
        if (!tb_enddate.Text.Equals("")) { where += " and dat_xml.query('//campos/frmQCDate').value('.', 'date')<='" + DateTime.Parse(tb_enddate.Text) + "'"; }
        // if (!lb_psbpo.SelectedValue.Trim().Equals("Select One")) where += " and CAST(dat_xml AS VARCHAR(8000)) like ('%<campo name=\"frmPSBPO_Client\">" + lb_psbpo.SelectedValue + "</campo>%')";
        //if (!lb_qc.SelectedValue.Trim().Equals("Select One")) where += " and CAST(dat_xml AS VARCHAR(8000)) like ('%<campo name=\"frmQCApproval\">" + lb_qc.SelectedValue + "</campo>%')";
        //if (!tb_county.Text.Trim().Equals("")) where += " and CAST(dat_xml AS VARCHAR(8000)) like ('%<campo name=\"frmCounty\">" + tb_county.Text + "</campo>%')";
        //if (!tb_enddate.Text.Trim().Equals("")) where = " and dat_xml like ('%<campo name=\"\">"+tb_enddate.Text+"</campo>%')";
        //if (!tb_startdate.Text.Trim().Equals("")) where = " and dat_xml like ('%<campo name=\"\">"+tb_startdate.Text+"</campo>%')";
        //  if (!tb_state.Text.Trim().Equals("")) where += " and CAST(dat_xml AS VARCHAR(8000)) like ('%<campo name=\"frmState\">" + tb_state.Text + "</campo>%')";
        /*if (!lb_date_choice.SelectedValue.Equals("Select One ")) {
            if (!tb_date.Text.Trim().Equals("")) where += " and CAST(dat_xml AS VARCHAR(8000)) like ('%<campo name=\""+lb_date_choice.SelectedValue+"\">" + tb_date.Text + "</campo>%')";
        }*/
       
        DataSet ds = (DataSet)DB.getDataMPower(where);
        Session["where"] = ds;
        gvd.DataSource = ds;
        gvd.DataBind();
    }
    protected void gvd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int id = int.Parse(gvd.Rows[index].Cells[1].Text);
            Response.Redirect("MPowerForm.aspx?id=" + id.ToString());
        }
    }
    protected void gvd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Style.Add("display", "none");
        }
        else if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Style.Add("display", "none");
            e.Row.Cells[2].Font.Size = 8;
            e.Row.Cells[3].Font.Size = 8;
            e.Row.Cells[4].Font.Size = 8;
            e.Row.Cells[5].Font.Size = 8;
            e.Row.Cells[6].Font.Size = 8;
            e.Row.Cells[7].Font.Size = 8;
            e.Row.Cells[8].Font.Size = 8;
            e.Row.Cells[9].Font.Size = 8;
            e.Row.Cells[10].Font.Size = 8;
            e.Row.Cells[11].Font.Size = 8;
        }

        string hoy = DateTime.Now.ToString("MM/dd/yy");
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (DataBinder.Eval(e.Row.DataItem, "Status").ToString().Equals("PENDING"))
            {
                e.Row.Cells[11].ForeColor = System.Drawing.Color.Red;
            }
            /*if (DataBinder.Eval(e.Row.DataItem, "Date").ToString().Equals(hoy.Substring(0, 5)))
            {
                //e.Row.BackColor = System.Drawing.Color.Red;
                e.Row.ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[3].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[4].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[5].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[7].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[8].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[9].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[10].ForeColor = System.Drawing.Color.Red;
                e.Row.Cells[11].ForeColor = System.Drawing.Color.Red;
               
            }*/
        }
    }
    protected void gvd_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DataSet ds = null;
        if (Session["where"] != null) ds = (DataSet)Session["where"];
        gvd.PageIndex = e.NewPageIndex;
        gvd.DataSource = ds;
        gvd.DataBind();
    }


}